<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8"%>
<%@ include file="../include/laypublic.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>资金拨付修改</title>
</head>
<style>
	.fieldset{
		margin-top: 5px;
		margin-bottom: 0px
	}
	.layui-legend{
		background-color:#5fb878;
		font-size:15px !important;
		width:140px;
	}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
	.layui-form-label{width: 180px;}
	.right{
		float:right;
	}
	.width106{
		width:106px;
	}
	.width60{
		width:60px;
	}
	.width30{
		width:30px;
	}
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
	    <div class="layui-tab-item layui-show">
	    	<div style="padding-top:15px;">&nbsp;</div>
			<form class="layui-form" id="addInfo_form" lay-filter="add_info_from">
				<fieldset class="layui-elem-field layui-field-title fieldset"  id="baseInfo">
				  <legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">基础信息  ></legend>
				  <div style="margin-top:20px">
				  	<div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label">申请公司</label>
					      	<div class="layui-input-inline">
					      		<input type="hidden" name="hrOrgId" value="${bindOrgId}">
								<input type="text" value="${bindOrgName}" style="border:0px;" readonly="readonly" class="layui-input">
					      	</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请部门</label>
				      		<div class="layui-input-inline">
				      			<input type="hidden" name="hrDeptId" value="${bindDeptId}">
								<input type="text" value="${bindDeptName}" style="border:0px;" readonly="readonly" class="layui-input">
				      		</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请人</label>
				      		<div class="layui-input-inline">
				        		<input type="hidden" name="hrStaffApplyId" readonly="readonly" value="${SESSION_KEY_USER_DETAIL.hrStaffId }">
				        		<input type="text" name="staffName" value="${SESSION_KEY_USER_DETAIL.realName }" readonly="readonly" style="border:0px" class="layui-input">
				      		</div>
					    </div>
				    </div>
				  	<div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label">费用归属公司</label>
					      	<div class="layui-input-inline">
					      	<select name="hrOrgBillId" lay-verify="required" lay-search>
					      		<option value="">请选择-归属公司</option>
				      				<c:if test="${not empty orgInfos }">
				              			<c:forEach items="${orgInfos}" var="info">
				              				<option  value= "${info.id }" <c:if test="${info.compName == fundsAppn.billName}">selected</c:if> >${info.compName }</option>
					            		</c:forEach>
				              		</c:if>
				      		</select>
					      	</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">拨付日期</label>
				      		<div class="layui-input-inline">
						        <input type="text" class="layui-input" value="<fmt:formatDate value="${fundsAppn.appnDate }" pattern="yyyy-MM-dd"/>" id="sent-laydate-appn" name="appnDate" lay-verify="required" readonly>
				      		</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">拨付金额总计（元）</label>
				      		<div class="layui-input-inline">
				      			<input type="hidden" name="fundsAppnId" value="${fundsAppn.id}" class="layui-input">
						        <input type="text" id="sumAppnAmount" value="${fundsAppn.sumAppnAmount }" name="sumAppnAmount" style="border:0px;" readonly="readonly" class="layui-input">
				      		</div>
					    </div>
				  		<div class="layui-inline">
					      	<label class="layui-form-label">备注</label>
					      	<div class="layui-input-inline">
					        	<input type="text" name="memo" value="${fundsAppn.memo }" maxlength="50" placeholder="请输入备注" class="layui-input">
					      	</div>
			    		</div>
				    </div>
		  		</div>
			</fieldset>
				<fieldset class="layui-elem-field layui-field-title fieldset" id="houseInfo">
			  		<legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">资金拨付明细  ></legend>
				  	<div style="margin-top:20px">
				  	<c:forEach items="${fundsAppn.fundsAppnDetails }" var="detail" varStatus="status">
				  		<div id="itemDiv">
					  		<div class="layui-form-item" id="itemRow${status.count }">
							    <div class="layui-inline width106">
							    	<p class="right">明细<label>${detail.showIdx }</label>：</p>
							    	<input type="hidden" name="showIdx" value="${detail.showIdx }"/>
							    	<%-- <input type="hidden" name="detailId" value="${detail.id }"/> --%>
							    </div>
							    <div class="layui-inline">
							      	<label class="layui-form-label width60">拨付方式</label>
							      	<div class="layui-input-inline" id="appnWaySelect" style="width:150px">
							        	<infopub:select entireName="-" id="appnWaySelect1" value="${detail.appnWay}" name="appnWay" laySearch="true" layVerify="required" type="dic" groupNo="A00010063"></infopub:select>
							      	</div>
							    </div>
							    <div class="layui-inline">
							      	<label class="layui-form-label width60">拨付类型</label>
							      	<div class="layui-input-inline" id="appnTypeSelect" style="width:150px">
							        	<infopub:select entireName="-" id="appnTypeSelect1" value="${detail.appnType}" name="appnType" laySearch="true" layVerify="required" type="dic" groupNo="A00010076"></infopub:select>
							      	</div>
							    </div>
							    <div class="layui-inline">
							      	<label class="layui-form-label width106">拨付金额（元）</label>
							      	<div class="layui-input-inline" style="width:100px">
							        	<input type="text" name="appnAmount" value="${detail.appnAmount }" maxlength="13" lay-verify="required|floatAmount" onblur="isNumberFloat(this);arabiaToCN(this,1);" autocomplete="off" class="layui-input">
							      	</div>
							    </div>
							    <div class="layui-inline">
							      	<label class="layui-form-label width30">摘要</label>
							      	<div class="layui-input-inline" style="width:120px">
							        	<input type="text" name="detailMemo" value="${detail.detailMemo }" maxlength="50" lay-verify="required" autocomplete="off" class="layui-input">
							      	</div>
							    </div>
							    <div class="layui-inline">
							    	<button type="button" class="layui-btn layui-btn-sm" id="addItemBtn${status.count }" onclick="addItemRow(this);"><i class="layui-icon">&#xe654;</i></button>
							   		<button type="button" class="layui-btn layui-btn-sm" <c:if test="${status.count == 1}">style="display:none"</c:if> id="delItemBtn${status.count }" onclick="deleteItemRow(${status.count });"><i class="layui-icon">&#xe640;</i></button>
							    </div>
						    </div>
					    </div>
					    </c:forEach>
				  	</div>
				</fieldset>
				<div class="layui-form-item" style="position: fixed ! important; right: 30px; bottom:45%;z-index:10000;">
					<div class="layui-input-block">
			      		<button class="layui-btn layui-bg-blue layui-btn-normal layui-btn-lg  layui-btn-radius" type="button" lay-submit lay-filter="addInfo_form_submit" id="addInfo_form_submit">
			      		<i class="layui-icon layui-icon-ok layuiadmin-button-btn"></i>
			      		提交
			      		</button>
			      	</div>
			    </div>
			</form>
  		</div>
	</div>
</div>
  <script>
	//静态资源路径前缀
	var staticPath = '${fileHttpPre}';
	var stateJSON = <infopub:dicJson groupNo="A00010028" />;
	
  layui.config({
    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set', 'upload', 'element', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,element = layui.element
    ,laydate = layui.laydate
    ,upload = layui.upload;
    
    laydate.render({
	      elem: '#sent-laydate-appn'
 		});
 	// 正则校验
	form.verify({
		//验证汉字
	    CHS:[
	    	/^[\u0391-\uFFE5]+$/
	    	,'只能输入汉字'
	  	],
	  	//大于0的正整数
	  	intNumber:[
			/^[1-9]\d*$/
			,'请输入正确的数字'
	  	],
	  	//数字只能有两位小数
	  	floatNumber:[
			/^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			,'请输入正确的数字'
	  	],
	  	//数字大于等于0，并只能有两位小数
	  	floatAmount:[
			/^([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			//var pattern=/^((\d+\.\d*[1-9]\d{1})|(\d*[1-9]\d*\.\d{2}))$/; 
			,'请输入正确的数字'
	  	]
	});
    
   	//监听提交
  	form.on('submit(addInfo_form_submit)', function(data){
  		$("#addInfo_form_submit").removeAttr("lay-submit").removeClass("layui-bg-blue").addClass("layui-btn-disabled");
  		layer.load();
       	//提交 Ajax 成功后，静态更新表格中的数据
       	$.ajax({
           	type : 'post',
			url : basePath + '/fundsAppn/editUdp',
           	data: $('#addInfo_form').serialize(),
			success:function(result){
				if(result.success){
					layer.closeAll('loading'); //关闭loading
					layer.msg('提交单据成功~',{icon: 1});
					//两秒后关闭当前页面
					setTimeout(function(){
						/**
						 * 查找父菜单，并刷新
						 */
						var topDoc = $(window.top.document);
						var path = basePath + '/fundsAppn/list';
						// 获取页面名称
						var name = topDoc.find(".layui-nav-item [lay-href^='" + path + "']").text();
						if(name == '' || name == null){
							name = '资金拨付';
						}
						window.parent.layui.index.openTabsPage(path, name);
						var iframe = window.parent.layui.admin.tabsBody(window.parent.layui.admin.tabsPage.index).find(".layadmin-iframe");
						iframe[0].contentWindow.location.reload(true);
						
						/**
						 * 关闭当前页
						 */
						var currPath = basePath + '/fundsAppn/edit';
						var layid = topDoc.find("li[lay-id^='" + currPath + "']").attr('lay-id');
						window.parent.layui.element.tabDelete('layadmin-layout-tabs', layid);//关闭当前页
				        }, 2000);
				}else{
					layer.closeAll('loading'); //关闭loading
  					$("#addInfo_form_submit").removeClass("layui-btn-disabled").addClass("layui-bg-blue").attr("lay-submit","");
					layer.msg(result.msg,{icon: 7});
			  	}
			}
       	});
   	});  
 	// data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
 	
  	//新增-付款公司select监听
    form.on('select(add_chooseOrg)', function(data){
    	var types=$("#hrOrgBillId").find("option:selected").attr("types");
    	var orgId = data.value;
    	if(types == '3'){//项目公司
    		$.ajax({
				type : "POST", //提交方式  
				url : "${ctx}/org/getProjectById",//路径  
				data : {
					"id" :orgId
				},//数据，这里使用的是Json格式进行传输  
				success : function(result) {//返回数据根据结果进行相应的处理 
					if(result.flag == true){
						if(result.preProject != null){
							$("#proName").val(result.preProject.proName);
							$("#proCode").val(result.preProject.proCode);
							$("#types").val(1);
						}
	    				$("#proDiv").show();
					}else{
						layer.msg('数据通信异常~',{icon: 0});
					}
				},
				error:function (result){ 
					layer.msg('数据通信异常~',{icon: 0});
				}   
			});
    	}else{
    		$("#proDiv").hide();
    	}
	});
  	
  });
  
	
	//新增一行
	function addItemRow(t){
		var $ = layui.$;
		var thisid = $(t).attr("id").substring(10);
		thisid = $("div[id=itemRow" + thisid + "]").find("div:first").find("label").html();
		thisid = parseInt(thisid);
		var nextIndex = parseInt(thisid) + 1;
		var maxInd = $("div[id^='itemRow']").length;
		$("div[id^='itemRow']").each(function(){
			var tId = $(this).attr("id").substring(7);
			tId = parseInt(tId);
		  	if(tId > maxInd){
		  		maxInd = parseInt(tId);
		  	}
		  	var thisIndex = $(this).find("div:first").find("label").html();
		  	thisIndex = parseInt(thisIndex);
		  	var nextId = thisIndex + 1;
		  	if(thisIndex > thisid) {
		  		$("div[id=itemRow" + tId + "]").find("div:first").find("label").html(nextId);
		  		$("div[id=itemRow" + tId + "]").find("div:first").find("input").val(nextId);
		  	}
		});
		var ind = maxInd + 1;
		var appnWayDiv = $("#appnWaySelect").clone();
		var appnTypeDiv = $("#appnTypeSelect").clone();
		var medalHtml = '';
		medalHtml += '<div class="layui-form-item" id="itemRow'+ind+'">';
		medalHtml += '	<div class="layui-inline width106">';
		medalHtml += '		<p class="right">明细<label>'+nextIndex+'</label>：</p>';
		medalHtml += '		<input type="hidden" name="showIdx" value="'+nextIndex+'"/>';
		/* medalHtml += '		<input type="hidden" name="detailId" value="'+nextIndex+'"/>'; */
		medalHtml += '	</div>';
		medalHtml += '	<div class="layui-inline">';
		medalHtml += '		<label class="layui-form-label width60">拨付方式</label>';
		medalHtml += '		<div class="layui-input-inline" style="width:150px;">';
   		/* medalHtml += appnWayDiv.html(); */
   		medalHtml += '		<select name="appnWay" lay-verify="required"><option value="">请选择-</option><option value="1">网银</option>';
   		medalHtml += '		<option value="2">现金</option><option value="3">支票</option><option value="4">银行承兑汇票</option>';
   		medalHtml += '		<option value="5">商业承兑汇票</option><option value="6">银行汇票</option><option value="7">总部代付工程款</option>';
   		medalHtml += '		<option value="8">其他</option></select>';
   		medalHtml += '		</div>';
   		medalHtml += '	</div>';
		medalHtml += '	<div class="layui-inline">';
		medalHtml += '		<label class="layui-form-label width60">拨付类型</label>';
		medalHtml += '		<div class="layui-input-inline" style="width:150px;">';
   		/* medalHtml += appnTypeDiv.html(); */
   		medalHtml += '		<select name="appnType" lay-verify="required"><option value="">请选择-</option><option value="01">工程款</option>';
   		medalHtml += '		<option value="02">建设资金</option><option value="03">前期费用</option><option value="04">日常经营借款</option><option value="05">投标保证金</option>';
   		medalHtml += '		<option value="06">投资款</option><option value="07">注册资本金</option><option value="08">资金调剂</option>';
   		medalHtml += '		<option value="09">资信证明</option></select>';
   		medalHtml += '		</div>';
   		medalHtml += '	</div>';
   		medalHtml += '	<div class="layui-inline">';
   		medalHtml += '		<label class="layui-form-label width106">拨付金额（元）</label>';
   		medalHtml += '		<div class="layui-input-inline" style="width:100px;">';
   		medalHtml += '			<input type="text" name="appnAmount" maxlength="13" lay-verify="required|floatAmount" onblur="isNumberFloat(this);" autocomplete="off" class="layui-input">';
   		medalHtml += '		</div>';
   		medalHtml += '	</div>';
   		medalHtml += '	<div class="layui-inline">';
   		medalHtml += '		<label class="layui-form-label width30">摘要</label>';
   		medalHtml += '		<div class="layui-input-inline" style="width:120px">';
   		medalHtml += '			<input type="text" name="detailMemo" maxlength="50" lay-verify="required" autocomplete="off" class="layui-input">';
   		medalHtml += '		</div>';
   		medalHtml += '	</div>';
		medalHtml += '	<div class="layui-inline">';
		medalHtml += '		<button type="button" class="layui-btn layui-btn-sm" id="addItemBtn'+ind+'" onclick="addItemRow(this);"><i class="layui-icon">&#xe654;</i></button>';
		medalHtml += '		<button type="button" class="layui-btn layui-btn-sm" id="delItemBtn'+ind+'" onclick="deleteItemRow('+ind+');"><i class="layui-icon">&#xe640;</i></button>';
		medalHtml += '	</div>';
		medalHtml += '</div>';
		$(t).parent().parent().after(medalHtml);
		layui.form.render();
	}
	
	//删除一行
	function deleteItemRow(id){
		var $ = layui.$;
		layui.layer.confirm('确定删除？',{icon:3, title :'提示'}, function(index){
 		   	var thisid = $("div[id=itemRow" + id + "]").find("div:first").find("label").html();
	    	thisid = parseInt(thisid);
	    	$("div[id^='itemRow']").each(function(){
				var tId = $(this).attr("id").substring(7);
				tId = parseInt(tId);
			  	var thisIndex = $("div[id=itemRow" + tId + "]").find("div:first").find("label").html();
			  	thisIndex = parseInt(thisIndex);
			  	var nextId = thisIndex - 1;
			  	if(thisIndex > thisid) {
			  		$("div[id=itemRow" + tId + "]").find("div:first").find("label").html(nextId);
			  		$("div[id=itemRow" + tId + "]").find("div:first").find("input").val(nextId);
			  	}
			});
	    	$("#itemRow"+id).remove();
	    	calcSumAmount();
			layui.layer.close(index);
 		});
	}
	
	//验证金额
	function isNumberFloat(obj){
		var $ = layui.$;
		if (obj.value == '' || obj.value == null) {
			return;
		}
		var rgx = /^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/;
		var boolrgx = rgx.test(obj.value);// true 
		if (!boolrgx){
			obj.value=0;
			layer.msg('请输入正确数字，且保留两位小数！');
		}
		calcSumAmount();
	}
	
	//计算总额
	function calcSumAmount(){
		var $ = layui.$;
		var totalAmount = 0;
  		$("input[name=appnAmount]").each(function(){
			var tId = $(this).val();
			if(tId != '' && !isNaN(tId)){
				totalAmount = totalAmount + parseFloat(tId);
			}
		});
		$("#sumAppnAmount").val(totalAmount.toFixed(2));
	}
  </script>
</body>
</html>

